package account;


import ch.qos.logback.core.util.FileUtil;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import pojo.Account;
import pojo.AccountAndOrder;
import pojo.Order;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/account")
public class AccountController {

    @Autowired
    OrderFeign orderFeign;

    @Autowired
    RedisTemplate redisTemplate;

    @GetMapping("/insertOrder")
    public Object insertOrder() throws InterruptedException {
        Long aaa = redisTemplate.getExpire("aaa");

        Order order = new Order();
        order.setAccountId(11L);
        order.setPort("123");
        List<Long> longs = new ArrayList<>();
        longs.add(1L);
        longs.add(3L);
        Account account = new Account();
        account.setAccountId(22L);
        AccountAndOrder accountAndOrder=new AccountAndOrder();
        accountAndOrder.setAccount(account);
        accountAndOrder.setOrder(order);

        File file = new File("E:\\javaPro\\prime-code-generator\\pom.xml");
        MultipartFile multipartFile =fileToMultipartFile(file);
        Object o = orderFeign.springUpload(multipartFile);
        // 模拟给当前账户下单
        return o;
    }

    public static MultipartFile fileToMultipartFile(File file) {
        String fieldName = "file";
        FileItemFactory factory = new DiskFileItemFactory(16, null);
        FileItem item = factory.createItem(fieldName, "multipart/form-data", true, file.getName());
        int bytesRead = 0;
        byte[] buffer = new byte[8192];
        try {
            FileInputStream fis = new FileInputStream(file);
            OutputStream os = item.getOutputStream();
            while ((bytesRead = fis.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new CommonsMultipartFile(item);
    }


    @GetMapping("/gzip")
    public Object gzip() {
        // 模拟给当前账户下单
        List<Account> list = new ArrayList<>();
        for (long i = 0L; i < 10000L; i++) {
            Account account = new Account();
            account.setAccountId(i);
            list.add(account);
        }
        return list;
    }
}
